package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SkuService;
import com.qingcheng.service.goods.SpuService;
import com.qingcheng.service.index.IndexService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Reference
    private SpuService spuService;

    @Reference
    private SkuService skuService;
    /**
     * 上架商品添加进elasticsearch中
     * @param spuId
     */
    @Override
    public void putawayToElasticSearch(String spuId) throws IOException {
        //获取sku集合
        Map searchMap = new HashMap<>();
        searchMap.put("spuId",spuId);
        searchMap.put("status","1");
        List<Sku> skuList = skuService.findList(searchMap);

        //封装存储内容
        BulkRequest bulkRequest = new BulkRequest();

        for (Sku sku : skuList) {
            IndexRequest indexRequest = new IndexRequest("sku","doc",sku.getId());
            Map skuMap = new HashMap();
            skuMap.put("name",sku.getName());
            skuMap.put("brandName",sku.getBrandName());
            skuMap.put("image",sku.getImage());
            skuMap.put("spuId",sku.getSpuId());
            skuMap.put("categoryName",sku.getCategoryName());
            skuMap.put("price",sku.getPrice());
            skuMap.put("createTime",sku.getCreateTime());
            skuMap.put("saleNum",sku.getSaleNum());
            skuMap.put("commentNum",sku.getCommentNum());
            String spec1 = sku.getSpec();
            Map specMap = JSON.parseObject(spec1, Map.class);
            skuMap.put("spec",specMap);
            indexRequest.source(skuMap);
            bulkRequest.add(indexRequest);
        }
        //上传数据到elasticsearch中
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        int status = bulk.status().getStatus();
        System.out.println(status);
        restHighLevelClient.close();
    }

    @Override
    public void delIndex(String spuId) {
        try {
            //获取sku集合
            Map searchMap = new HashMap<>();
            searchMap.put("spuId",spuId);
            searchMap.put("status","1");
            List<Sku> skuList = skuService.findList(searchMap);
            for (Sku sku : skuList) {
                DeleteRequest deleteRequest = new DeleteRequest("sku","doc",sku.getId());
                DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
                int status = delete.status().getStatus();
                System.out.println(status);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                restHighLevelClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
